#!/bin/bash

# 重置函数
rsetMamp() {
  sudo rm -rf "${HOME}"/Library/WebKit/de.appsolute.mamppro
  sudo rm -rf "${HOME}"/Library/Preferences/de.appsolute.mamppro.plist
  sudo rm -rf "${HOME}"/Library/Application\ Support/de.appsolute.mamppro
  sudo rm -rf "${HOME}"/Library/Application\ Support/de.appsolute.MAMP
  sudo rm -rf "${HOME}"/Library/Application\ Support/appsolute
  sudo rm -rf "${HOME}"/Library/Caches/de.appsolute.mamppro
  sudo rm -rf "${HOME}"/Library/Caches/com.plausiblelabs.crashreporter.data/de.appsolute.MAMP
  sudo rm -rf "${HOME}"/Library/Caches/com.plausiblelabs.crashreporter.data/de.appsolute.mamppro
  sudo rm -rf /Library/Application\ Support/appsolute
  sudo rm -rf /Library/PrivilegedHelperTools/de.appsolute.mampprohelper
  sudo rm -rf /Library/LaunchDaemons/de.appsolute.mampprohelper.plist
  cd "${HOME}"/Library/Application\ Support/CrashReporter/
  find . -name 'MAMP*' -exec rm -f {} \;
  cd /Library/Application\ Support/CrashReporter
  find . -name 'de.appsolute.*' -exec rm -f {} \;
  echo '重置完成,请先重新安装 MAMP Pro ,安装完成后打开 MAMP Pro。'
  echo '最后执行还原操作'
}
# 备份函数
beifen() {
  time=$(date +%F'_'%T)
  # 设置备份文件夹名
  name="${bakNamePrefix}${time}"
  cd ${dir} || exit
  # 创建
  mkdir "${dir}/${name}"
  # 备份站点配置文件
  cd "${HOME}"/Library/Application\ Support/appsolute/MAMP\ PRO/ || exit
  zip mamp.set.zip settings6.plist structure6.plist
  cp -f mamp.set.zip "${dir}"/"${name}"
  rm -rf mamp.set.zip
  # 备份数据库
  cd /Library/Application\ Support/appsolute/MAMP\ PRO/ || exit
  zip mamp.db.zip -r db
  cp -f mamp.db.zip "${dir}"/"${name}"
  rm -rf mamp.db.zip
  # 备份完成
  cd "$dir" || exit
  read -n1 -r -p "备份完成是否开始重置操作?[y/n]" info
  echo ''
  case $info in
  y | Y)
    rsetMamp
    ;;
  n | N)
    echo '重置取消'
    exit
    ;;
  esac
}

# 设置当前目录
dir=$(pwd)
# 备份文件地址名前缀
bakNamePrefix='mamp_bak_'
echo 'MAMP Pro 重置试用脚本 0.0.2'
echo '1)备份操作'
echo '2)重置操作'
echo '3)还原操作'
read -n1 -r -p "请选择操作模式?" info
echo ''
case $info in
1)
  # 开始备份
  beifen
  ;;
2)
  # 重置操作
  rsetMamp
  ;;
3)
  # 还原操作
  # 查找文件并遍历输出
  # 定义数组
  bakArr=()
  num=0
  # 查找本地备份文件
  for d in $(find . -type d -name "${bakNamePrefix}*"); do
    #根据数组长度添加下标
    bakArr[${#bakArr[@]}]=${d}
    echo "${num})${d}"
    let num++
  done
  # 大于 0
  if [ "${#bakArr[@]}" -gt 0 ]; then
    read -n1 -r -p "请选择还原的备份对象序号:" i
    echo ''
    echo "开始还原选择备份:${bakArr[i]}"
    cp "${bakArr[i]}"/mamp.set.zip "${HOME}"/Library/Application\ Support/appsolute/MAMP\ PRO/mamp.set.zip
    cd "${HOME}"/Library/Application\ Support/appsolute/MAMP\ PRO/ || exit
    unzip -o mamp.set.zip
    rm -rf mamp.set.zip
    cd "$dir" || exit
    sudo cp "${bakArr[i]}"/mamp.db.zip /Library/Application\ Support/appsolute/MAMP\ PRO/mamp.db.zip
    cd /Library/Application\ Support/appsolute/MAMP\ PRO/ || exit
    unzip -o mamp.db.zip
    rm -rf mamp.db.zip
    cd "$dir" || exit
    echo '还原完成,请注意端口等软件设置已经重置,需要重新设置'
    echo '如修改站点，无法保存，或修改站点配置导致软件错误，请检查并确认站点配置文件，当前用户是否有读写权限'
    echo '执行 ：cd ~/Library/Application\ Support/appsolute/MAMP\ PRO/'
    echo '检查 settings6.plist structure6.plist 确保有被 MAMP Pro.app 读写的权限'
  else
    echo '没有找到备份文件'
    read -n1 -r -p "是否执行备份操作?[y/n]" info
    echo ''
    case $info in
    y | Y)
      beifen
      ;;
    n | N)
      echo '备份取消'
      exit
      ;;
    esac
  fi

  ;;
esac
